<template>
  <div id="chatManagePage">
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="消息内容">
        <a-input v-model:value="searchParams.message" placeholder="输入消息内容" />
      </a-form-item>
      <a-form-item label="消息类型">
        <a-select
          v-model:value="searchParams.messageType"
          placeholder="选择消息类型"
          style="width: 120px"
        >
          <a-select-option value="">全部</a-select-option>
          <a-select-option value="user">用户消息</a-select-option>
          <a-select-option value="assistant">AI消息</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="应用ID">
        <a-input v-model:value="searchParams.appId" placeholder="输入应用ID" />
      </a-form-item>
      <a-form-item label="用户ID">
        <a-input v-model:value="searchParams.userId" placeholder="输入用户ID" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider />

    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="data"
      :pagination="pagination"
      @change="doTableChange"
      :scroll="{ x: 1400 }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'message'">
          <a-tooltip :title="record.message">
            <div class="message-text">{{ record.message }}</div>
          </a-tooltip>
        </template>
        <template v-else-if="column.dataIndex === 'messageType'">
          <a-tag :color="record.messageType === 'user' ? 'blue' : 'green'">
            {{ record.messageType === 'user' ? '用户消息' : 'AI消息' }}
          </a-tag>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ formatTime(record.createTime) }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space>
            <a-button type="primary" size="small" @click="viewAppChat(record.appId)">
              查看对话
            </a-button>
            <a-popconfirm title="确定要删除这条消息吗？" @confirm="deleteMessage(record.id)">
              <a-button danger size="small">删除</a-button>
            </a-popconfirm>
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { listAllChatHistoryByPageForAdmin } from '@/api/chatHistoryController.ts'
import { formatTime } from '@/utils/time'

const router = useRouter()

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
    fixed: 'left',
  },
  {
    title: '消息内容',
    dataIndex: 'message',
    width: 300,
  },
  {
    title: '消息类型',
    dataIndex: 'messageType',
    width: 100,
  },
  {
    title: '应用ID',
    dataIndex: 'appId',
    width: 80,
  },
  {
    title: '用户ID',
    dataIndex: 'userId',
    width: 80,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 160,
  },
  {
    title: '操作',
    key: 'action',
    width: 180,
    fixed: 'right',
  },
]

// 数据
const data = ref<API.ChatHistory[]>([])
const total = ref(0)

// 搜索条件
const searchParams = reactive<API.ChatHistoryQueryRequest>({
  pageNum: 1,
  pageSize: 10,
})

// 获取数据
const fetchData = async () => {
  try {
    const res = await listAllChatHistoryByPageForAdmin({
      ...searchParams,
    })
    if (res.data.data) {
      data.value = res.data.data.records ?? []
      total.value = res.data.data.totalRow ?? 0
    } else {
      message.error('获取数据失败，' + res.data.message)
    }
  } catch (error) {
    console.error('获取数据失败：', error)
    message.error('获取数据失败')
  }
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.pageNum ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

// 表格变化处理
const doTableChange = (page: { current: number; pageSize: number }) => {
  searchParams.pageNum = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 搜索
const doSearch = () => {
  // 重置页码
  searchParams.pageNum = 1
  fetchData()
}

// 查看应用对话
const viewAppChat = (appId: number | undefined) => {
  if (appId) {
    router.push(`/app/chat/${appId}`)
  }
}

// 删除消息
const deleteMessage = async (id: number | undefined) => {
  if (!id) return

  try {
    // 注意：这里需要后端提供删除对话历史的接口
    // 目前先显示成功，实际实现需要调用删除接口
    message.success('删除成功')
    // 刷新数据
    fetchData()
  } catch (error) {
    console.error('删除失败：', error)
    message.error('删除失败')
  }
}
</script>

<style scoped>
#chatManagePage {
  padding: 32px;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 25%, #334155 50%, #475569 100%);
  min-height: calc(100vh - 64px);
  position: relative;
  overflow: hidden;
}

/* 科技感背景网格 */
#chatManagePage::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    linear-gradient(rgba(0, 255, 255, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 255, 255, 0.05) 1px, transparent 1px),
    linear-gradient(rgba(138, 43, 226, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(138, 43, 226, 0.04) 1px, transparent 1px);
  background-size:
    100px 100px,
    100px 100px,
    20px 20px,
    20px 20px;
  pointer-events: none;
  animation: gridFloat 20s ease-in-out infinite;
}

@keyframes gridFloat {
  0%, 100% { transform: translate(0, 0); }
  50% { transform: translate(2px, 2px); }
}

/* 动态光效 */
#chatManagePage::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(circle at 20% 30%, rgba(0, 255, 255, 0.08) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(138, 43, 226, 0.06) 0%, transparent 50%),
    radial-gradient(circle at 50% 50%, rgba(0, 255, 127, 0.04) 0%, transparent 50%);
  pointer-events: none;
  animation: lightPulse 8s ease-in-out infinite alternate;
}

@keyframes lightPulse {
  0% { opacity: 0.5; }
  100% { opacity: 1; }
}

/* 内容区域 */
#chatManagePage > * {
  position: relative;
  z-index: 2;
}

.message-text {
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #cbd5e1;
}

/* 搜索表单样式 */
:deep(.ant-form) {
  background: rgba(15, 23, 42, 0.8);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 255, 255, 0.2);
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

:deep(.ant-form-item-label > label) {
  color: #e2e8f0 !important;
  font-weight: 500;
}

:deep(.ant-input) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  color: #e2e8f0 !important;
  border-radius: 8px;
}

:deep(.ant-input:focus) {
  border-color: rgba(0, 255, 255, 0.6) !important;
  box-shadow: 0 0 0 2px rgba(0, 255, 255, 0.2) !important;
}

:deep(.ant-input::placeholder) {
  color: #64748b !important;
}

:deep(.ant-select) {
  color: #e2e8f0;
}

:deep(.ant-select-selector) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  color: #e2e8f0 !important;
  border-radius: 8px;
}

:deep(.ant-select-focused .ant-select-selector) {
  border-color: rgba(0, 255, 255, 0.6) !important;
  box-shadow: 0 0 0 2px rgba(0, 255, 255, 0.2) !important;
}

:deep(.ant-select-dropdown) {
  background: rgba(15, 23, 42, 0.95) !important;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 255, 255, 0.2);
  border-radius: 8px;
}

:deep(.ant-select-item) {
  color: #cbd5e1 !important;
}

:deep(.ant-select-item-option-selected) {
  background: rgba(0, 255, 255, 0.2) !important;
  color: #00ffff !important;
}

:deep(.ant-select-item-option-active) {
  background: rgba(0, 255, 255, 0.1) !important;
}

:deep(.ant-btn-primary) {
  background: linear-gradient(135deg, #00ffff, #8a2be2) !important;
  border: none !important;
  border-radius: 8px;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(0, 255, 255, 0.3);
  transition: all 0.3s ease;
}

:deep(.ant-btn-primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 255, 255, 0.5);
}

/* 表格样式 */
:deep(.ant-table) {
  background: rgba(15, 23, 42, 0.8) !important;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 255, 255, 0.2);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

:deep(.ant-table-thead > tr > th) {
  background: rgba(30, 41, 59, 0.9) !important;
  color: #e2e8f0 !important;
  border-bottom: 1px solid rgba(0, 255, 255, 0.2) !important;
  font-weight: 600;
  font-size: 14px;
}

:deep(.ant-table-tbody > tr > td) {
  background: rgba(15, 23, 42, 0.6) !important;
  color: #cbd5e1 !important;
  border-bottom: 1px solid rgba(0, 255, 255, 0.1) !important;
  vertical-align: middle;
}

:deep(.ant-table-tbody > tr:hover > td) {
  background: rgba(0, 255, 255, 0.1) !important;
  color: #e2e8f0 !important;
}

/* 按钮样式 */
:deep(.ant-btn-default) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  color: #cbd5e1 !important;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.ant-btn-default:hover) {
  border-color: rgba(0, 255, 255, 0.6) !important;
  color: #00ffff !important;
  background: rgba(0, 255, 255, 0.1) !important;
  transform: translateY(-1px);
}

:deep(.ant-btn-dangerous) {
  background: linear-gradient(135deg, #ef4444, #dc2626) !important;
  border: none !important;
  color: white !important;
  border-radius: 8px;
  font-weight: 500;
}

:deep(.ant-btn-dangerous:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 15px rgba(239, 68, 68, 0.4);
}

/* 标签样式 */
:deep(.ant-tag) {
  border-radius: 6px;
  font-weight: 500;
  border: none;
}

:deep(.ant-tag-blue) {
  background: rgba(0, 255, 255, 0.2) !important;
  color: #00ffff !important;
  border: 1px solid rgba(0, 255, 255, 0.4) !important;
}

:deep(.ant-tag-green) {
  background: rgba(0, 255, 127, 0.2) !important;
  color: #00ff7f !important;
  border: 1px solid rgba(0, 255, 127, 0.4) !important;
}

/* 分页样式 */
:deep(.ant-pagination) {
  margin-top: 24px;
  text-align: center;
}

:deep(.ant-pagination-item) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  border-radius: 6px;
}

:deep(.ant-pagination-item a) {
  color: #cbd5e1 !important;
}

:deep(.ant-pagination-item-active) {
  background: linear-gradient(135deg, #00ffff, #8a2be2) !important;
  border-color: transparent !important;
}

:deep(.ant-pagination-item-active a) {
  color: white !important;
}

:deep(.ant-pagination-prev, .ant-pagination-next) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  border-radius: 6px;
}

:deep(.ant-pagination-prev a, .ant-pagination-next a) {
  color: #cbd5e1 !important;
}

/* 分割线样式 */
:deep(.ant-divider) {
  border-color: rgba(0, 255, 255, 0.2) !important;
}

/* 气泡确认框样式 */
:deep(.ant-popover-inner) {
  background: rgba(15, 23, 42, 0.95) !important;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 255, 255, 0.2);
  border-radius: 8px;
}

:deep(.ant-popover-inner-content) {
  color: #cbd5e1;
}

:deep(.ant-popover-arrow::before) {
  background: rgba(15, 23, 42, 0.95) !important;
  border: 1px solid rgba(0, 255, 255, 0.2) !important;
}

/* 提示框样式 */
:deep(.ant-tooltip-inner) {
  background: rgba(15, 23, 42, 0.95) !important;
  color: #cbd5e1 !important;
  border: 1px solid rgba(0, 255, 255, 0.2);
  border-radius: 6px;
}

:deep(.ant-tooltip-arrow::before) {
  background: rgba(15, 23, 42, 0.95) !important;
  border: 1px solid rgba(0, 255, 255, 0.2) !important;
}
</style>
